package cn.cvs.servlet;


import cn.cvs.pojo.Powerbattery;
import cn.cvs.service.PowerbatteryService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("PwbtyServlet")
public class PwbtyServlet extends HttpServlet {


    ApplicationContext context= WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    PowerbatteryService powerbatteryService= context.getBean(PowerbatteryService.class);


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String arr=request.getParameter("arr");
        switch (arr) {
            case "1":
                show(request,response);
            case "2":
                delete(request,response);
            case "3":
                selectId(request,response);
            case "4":
                update(response,request);

        }

    }

    public void update(HttpServletResponse response,HttpServletRequest request) throws IOException {
        String craName=request.getParameter("craName");
        String cityName=request.getParameter("cityName");
        String userName=request.getParameter("userName");
        int pressureValue= Integer.parseInt(request.getParameter("pressureValue"));
        String time=request.getParameter("time");
        int id= Integer.parseInt(request.getParameter("id"));
        Powerbattery powerbattery=new Powerbattery();
        powerbattery.setId(id);
        powerbattery.setCarName(craName);
        powerbattery.setCitName(cityName);
        powerbattery.setUserName(userName);
        powerbattery.setPressureValue(pressureValue);
        powerbattery.setTime(time);
        PrintWriter out= response.getWriter();
        int update = powerbatteryService.update(powerbattery);
        if (update>0){
            out.print("<script>" +
                    "  alert('修改成功')" +
                    "  location.href=index.jsp" +
                    "</script>");
        }else {
            out.print("<script>" +
                    "  alert('修改失败')" +
                    "  history.back()" +
                    "</script>");
        }
    }

    public void selectId(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        int id= Integer.parseInt(request.getParameter("id"));
        Powerbattery powerbattery=powerbatteryService.selectId(id);
        request.setAttribute("p",powerbattery);
        request.getRequestDispatcher("demo2.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    public void show(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

        List<Powerbattery> list=powerbatteryService.list();
        if (list!=null){
            request.setAttribute("list",list);
            request.getRequestDispatcher("index.jsp").forward(request,response);
        }
    }

    public void delete(HttpServletRequest request,HttpServletResponse response) throws  IOException {
        PrintWriter out= response.getWriter();
        int id= Integer.parseInt(request.getParameter("id"));
        int count= powerbatteryService.delete(id);
        if (id>0){
            out.print(true);
        }else {
            out.print(false);
        }
    }
}
